﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class UdfReportSettingDA : IUdfReportSettingDA
    {
        private string DATA_SOURCE_NAME1 = "@DATA_SOURCE_NAME1";
        private string DATA_SOURCE_NAME2 = "@DATA_SOURCE_NAME2";
        private string DATA_SQL = "@DATA_SQL";
        private string REMARK = "@REMARK";
        private string REPORT_FILE = "@REPORT_FILE";
        private string REPORT_ID = "@REPORT_ID";
        private string REPORT_NAME = "@REPORT_NAME";
        private string SQL_CHECK_UDF_REPORT_SETTING_ID_UNIQUE = " SELECT COUNT(1) FROM @_@UDF_REPORT_SETTING WHERE REPORT_ID = @REPORT_ID ";
        private string SQL_DELETE_UDF_REPORT_SETTING = " DELETE FROM @_@UDF_REPORT_SETTING WHERE REPORT_ID = @REPORT_ID ";
        private string SQL_INSERT_UDF_REPORT_SETTING = " INSERT INTO @_@UDF_REPORT_SETTING ( WH_ID, REPORT_ID, REPORT_NAME, REPORT_FILE, DATA_SOURCE_NAME1, DATA_SOURCE_NAME2, REMARK, DATA_SQL) VALUES ( @WH_ID, @REPORT_ID, @REPORT_NAME, @REPORT_FILE, @DATA_SOURCE_NAME1, @DATA_SOURCE_NAME2, @REMARK, @DATA_SQL )  ";
        private string SQL_SELECT_ALL_UDF_REPORT_SETTING = " SELECT  ROWID, WH_ID, REPORT_ID, REPORT_NAME, REPORT_FILE, DATA_SOURCE_NAME1, DATA_SOURCE_NAME2, REMARK, DATA_SQL  FROM  @_@UDF_REPORT_SETTING ";
        private string SQL_UPDATE_UDF_REPORT_SETTING = " UPDATE @_@UDF_REPORT_SETTING SET WH_ID = @WH_ID, REPORT_NAME = @REPORT_NAME, REPORT_FILE = @REPORT_FILE, DATA_SOURCE_NAME1 = @DATA_SOURCE_NAME1, DATA_SOURCE_NAME2 = @DATA_SOURCE_NAME2, REMARK = @REMARK, DATA_SQL = @DATA_SQL WHERE REPORT_ID = @REPORT_ID  ";
        private string WH_ID = "@WH_ID";

        public bool CheckUdfReportSettingIDUnique(string udfReportSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfReportSettingID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_UDF_REPORT_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckUdfReportSettingIDUnique(DataBase dataBase, DbTransaction tran, string udfReportSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfReportSettingID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_UDF_REPORT_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteUdfReportSetting(List<string> udfReportSettingID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < udfReportSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfReportSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_UDF_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteUdfReportSetting(DataBase dataBase, DbTransaction tran, List<string> udfReportSettingID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < udfReportSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfReportSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_UDF_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllUdfReportSetting(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_REPORT_SETTING.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllUdfReportSetting(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_REPORT_SETTING.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public UdfReportSettingInfo GetUdfReportSettingByID(string udfreportsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_REPORT_SETTING.Replace("@_@", whLoginID) + " WHERE REPORT_ID = @REPORT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfreportsettingID) };
            UdfReportSettingInfo udfReportSettingInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    udfReportSettingInfo = new UdfReportSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["REPORT_ID"].ToString(), reader["REPORT_NAME"].ToString(), reader["REPORT_FILE"].ToString(), reader["DATA_SOURCE_NAME1"].ToString(), reader["DATA_SOURCE_NAME2"].ToString(), reader["REMARK"].ToString(), reader["DATA_SQL"].ToString());
                }
            }
            return udfReportSettingInfo;
        }

        public UdfReportSettingInfo GetUdfReportSettingByID(DataBase dataBase, DbTransaction tran, string udfreportsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_REPORT_SETTING.Replace("@_@", whLoginID) + " WHERE REPORT_ID = @REPORT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfreportsettingID) };
            UdfReportSettingInfo udfReportSettingInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                udfReportSettingInfo = new UdfReportSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["REPORT_ID"].ToString(), reader["REPORT_NAME"].ToString(), reader["REPORT_FILE"].ToString(), reader["DATA_SOURCE_NAME1"].ToString(), reader["DATA_SOURCE_NAME2"].ToString(), reader["REMARK"].ToString(), reader["DATA_SQL"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return udfReportSettingInfo;
        }

        public DataSet GetUdfReportSettingByQueryList(List<string> sqlWhere, UdfReportSettingQueryEntity udfreportsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_UDF_REPORT_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!udfreportsettingQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, udfreportsettingQuery.CurrentPage, udfreportsettingQuery.PageSize, udfreportsettingQuery.SortField, udfreportsettingQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetUdfReportSettingByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, UdfReportSettingQueryEntity udfreportsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_UDF_REPORT_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, udfreportsettingQuery.CurrentPage, udfreportsettingQuery.PageSize, udfreportsettingQuery.SortField, udfreportsettingQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertUdfReportSetting(UdfReportSettingInfo udfReportSettingInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                udfReportSettingInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_UdfReportSetting_Parameters(udfReportSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_UDF_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertUdfReportSetting(DataBase dataBase, DbTransaction tran, UdfReportSettingInfo udfReportSettingInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                udfReportSettingInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_UdfReportSetting_Parameters(udfReportSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_UDF_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_UdfReportSetting_Parameters(UdfReportSettingInfo udfReportSettingInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.REPORT_ID, SqlDbType.VarChar, 30), new SqlParameter(this.REPORT_NAME, SqlDbType.VarChar, 200), new SqlParameter(this.REPORT_FILE, SqlDbType.VarChar, 200), new SqlParameter(this.DATA_SOURCE_NAME1, SqlDbType.VarChar, 200), new SqlParameter(this.DATA_SOURCE_NAME2, SqlDbType.VarChar, 200), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.DATA_SQL, SqlDbType.VarChar, 0x1388) };
            if (!string.IsNullOrEmpty(udfReportSettingInfo.WhID))
            {
                paramArray[0].Value = udfReportSettingInfo.WhID.Substring(0, udfReportSettingInfo.WhID.Length - 1);
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = udfReportSettingInfo.ReportID;
            if (!string.IsNullOrEmpty(udfReportSettingInfo.ReportName))
            {
                paramArray[2].Value = udfReportSettingInfo.ReportName;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = udfReportSettingInfo.ReportFile;
            paramArray[4].Value = udfReportSettingInfo.DataSourceName1;
            paramArray[5].Value = udfReportSettingInfo.DataSourceName2;
            if (!string.IsNullOrEmpty(udfReportSettingInfo.Remark))
            {
                paramArray[6].Value = udfReportSettingInfo.Remark;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            paramArray[7].Value = udfReportSettingInfo.DataSql;
            return paramArray;
        }

        public int UpdateUdfReportSetting(UdfReportSettingInfo udfReportSettingInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                udfReportSettingInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_UdfReportSetting_Parameters(udfReportSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_UDF_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateUdfReportSetting(DataBase dataBase, DbTransaction tran, UdfReportSettingInfo udfReportSettingInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                udfReportSettingInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_UdfReportSetting_Parameters(udfReportSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_UDF_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

